#include <stdio.h>
#include <avr/io.h>
#include <avr/iom644.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "usart_utils.h"

void	ADC_Init();
int		ADC_Read();
double	convert_preasure(int value);

int
main(void)
{
	int aux;
	char string[40] = "";

	USART_Init(25);
	ADC_Init();
	DDRB = 0xFF;

	USART_Send_String("Iniciando la aplicacion!");

	while(1) {
		aux = ADC_Read();
		sprintf(string, "Dato: %d \n\r", aux);
		USART_Send_String(string);
		_delay_ms(100);
	}
}

void
ADC_Init() {

	ADMUX = (0 << REFS1)|(1 << REFS0)|(0 << ADLAR)|(0 << MUX4)|(0 << MUX3)|(0 << MUX2)|(0 << MUX1)|(0 << MUX0);
	ADCSRA = (1 << ADEN)|(1 << ADPS2)|(0 << ADPS1)|(0 << ADPS0);
}

int
ADC_Read() {

	ADCSRA = ADCSRA|(1 << ADSC);

	while(ADCSRA&(1<<ADSC)){
		;
	}

	return (ADCL|(ADCH << 8));
}

double
convert_preasure(int value) {

	return (((value/1024)-0.04)/0.9);
}
